home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Oh!X 2001 Spring
/
Oh!X 2001 Spring Special CD-ROM (Japan).7z
/
Oh!X 2001 Spring Special CD-ROM (Japan) (Track 1).bin
/
ERI
/
eri2bmp.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2000-12-01
|
6KB
|
255 lines
/*****************************************************************************
恵理ちゃん → ビットマップ 変換プログラム
2000/11/27
-----------------------------------------------------------------------------
Copyright (C) 2000 Leshade Entis. All rights reserved.
*****************************************************************************/
//
// ヘッダの読み込み
//////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <math.h>
#include <memory.h>
typedef signed short int SWORD ;
typedef signed long int SDWORD ;
typedef unsigned __int64 UINT64 ;
#include "experi.h"
//
// 入力コンテキストの宣言と定義
//////////////////////////////////////////////////////////////////////////////
class MyDecodeContext : public RLHDecodeContext
{
ERIFile * m_pfile ;
public:
// 構築関数
MyDecodeContext( ERIFile * pfile )
: RLHDecodeContext( 0x1000 ), m_pfile( pfile ) { }
// データ入力関数
virtual ULONG ReadNextData( PBYTE ptrBuffer, ULONG nBytes ) ;
} ;
ULONG MyDecodeContext::ReadNextData( PBYTE ptrBuffer, ULONG nBytes )
{
return m_pfile->Read( ptrBuffer, nBytes ) ;
}
//
// メモリアロケーション
//////////////////////////////////////////////////////////////////////////////
PVOID eriAllocateMemory( DWORD dwBytes )
{
return malloc( dwBytes ) ;
}
void eriFreeMemory( PVOID pMemory )
{
free( pMemory ) ;
}
//
// エントリポイント
//////////////////////////////////////////////////////////////////////////////
int main( int argc, char * argv[] )
{
//
// プログラム見出し表示
//
printf( "恵理ちゃん → ビットマップ 変換プログラム\n" ) ;
printf( "Copyright (C) 2000 Leshade Entis. All rights reserved.\n" ) ;
printf( "\n" ) ;
if ( argc < 3 )
{
//
// 書式表示
//
printf( "書式 : eri2bmp <ERIファイル> <BMPファイル>\n" ) ;
printf( "\n" ) ;
printf( " ERIファイル : 変換するERIファイル名を指定します。\n" ) ;
printf( " BMPファイル : 変換後のBMPファイル名を指定します。\n" ) ;
printf( "\n" ) ;
return 0 ;
}
//
// ERIファイルを開く
//
EReadFile rf ;
if ( !rf.Open( argv[1] ) )
{
printf( "\"%s\" を開けませんでした。\n\n", argv[1] ) ;
return 1 ;
}
ERIFile erif ;
if ( !erif.Open( &rf ) )
{
printf( "\"%s\" は恵理ちゃんではありません。\n\n", argv[1] ) ;
return 2 ;
}
//
// ビットマップを展開するための準備
//
RASTER_IMAGE_INFO rii ;
rii.fdwFormatType = erif.m_InfoHeader.fdwFormatType ;
rii.nImageWidth = erif.m_InfoHeader.nImageWidth ;
rii.nImageHeight = abs( erif.m_InfoHeader.nImageHeight ) ;
rii.dwBitsPerPixel = erif.m_InfoHeader.dwBitsPerPixel ;
rii.BytesPerLine =
((rii.nImageWidth * rii.dwBitsPerPixel + 0x1f) & ~0x1f) >> 3 ;
rii.ptrImageArray =
(PBYTE) malloc( rii.BytesPerLine * rii.nImageHeight ) ;
//
printf( "現在展開中です・・・" ) ;
//
// 展開開始
//
ERIDecoder decoder ;
MyDecodeContext context( &erif ) ;
if ( decoder.Initialize( erif.m_InfoHeader ) )
{
printf( "初期化に失敗しました。\n" ) ;
free( rii.ptrImageArray ) ;
return 3 ;
}
if ( decoder.DecodeImage( rii, context, false ) )
{
printf( "失敗しました。\n" ) ;
free( rii.ptrImageArray ) ;
return 4 ;
}
//
printf( "終了しました。\n" ) ;
//
// BMPファイルを開く
//
FILE * bmpf = fopen( argv[2], "wb" ) ;
if ( bmpf == NULL )
{
printf( "\"%s\" を開けませんでした。\n\n", argv[2] ) ;
free( rii.ptrImageArray ) ;
return 5 ;
}
bool fError = false ;
printf( "現在書き出し中です・・・" ) ;
do
{
//
// ビットマップファイルヘッダを書き出す
//
int pltlen = 0 ; // パレットの長さ
if ( rii.dwBitsPerPixel <= 8 )
{
pltlen = (1 << rii.dwBitsPerPixel) ;
}
BITMAPFILEHEADER bmfh ;
bmfh.bfType = *((WORD*)"BM") ;
bmfh.bfOffBits = sizeof(BITMAPFILEHEADER)
+ sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * pltlen ;
bmfh.bfReserved1 = 0 ;
bmfh.bfReserved2 = 0 ;
bmfh.bfSize = bmfh.bfOffBits + rii.BytesPerLine * rii.nImageHeight ;
if ( fwrite( &bmfh, sizeof(bmfh), 1, bmpf ) < 1 )
{
fError = true ;
break ;
}
//
// ビットマップ情報ヘッダを書き出す
//
BITMAPINFOHEADER bmih ;
memset( &bmih, 0, sizeof(bmih) ) ;
bmih.biSize = sizeof(bmih) ;
bmih.biWidth = rii.nImageWidth ;
bmih.biHeight = rii.nImageHeight ;
bmih.biPlanes = 1 ;
bmih.biBitCount = (WORD) rii.dwBitsPerPixel ;
bmih.biCompression = BI_RGB ;
bmih.biSizeImage = rii.BytesPerLine * rii.nImageHeight ;
if ( fwrite( &bmih, sizeof(bmih), 1, bmpf ) < 1 )
{
fError = true ;
break ;
}
//
// パレットテーブルを書き出す
//
if ( pltlen > 0 )
{
RGBQUAD rgbTable[0x100] ;
if ( rii.fdwFormatType & ERI_WITH_PALETTE )
{
memcpy( rgbTable,
erif.m_PaletteTable, sizeof(RGBQUAD) * pltlen ) ;
}
else
{
BYTE step = (BYTE)(1 << (8 - rii.dwBitsPerPixel)) ;
BYTE value = 0 ;
for ( int i = 0; i < pltlen; i ++ )
{
rgbTable[i].rgbBlue = value ;
rgbTable[i].rgbGreen = value ;
rgbTable[i].rgbRed = value ;
rgbTable[i].rgbReserved = 0 ;
value += step ;
}
}
if ( fwrite( rgbTable,
sizeof(RGBQUAD), pltlen, bmpf ) < (size_t) pltlen )
{
fError = true ;
break ;
}
}
//
// ビットマップ配列を書き出す
//
if ( fwrite( rii.ptrImageArray, bmih.biSizeImage, 1, bmpf ) < 1 )
{
fError = true ;
break ;
}
}
while ( false ) ;
//
fclose( bmpf ) ;
//
if ( fError )
{
printf( "失敗しました。\n" ) ;
free( rii.ptrImageArray ) ;
return 6 ;
}
printf( "終了\n\n" ) ;
//
// 後始末
//
free( rii.ptrImageArray ) ;
return 0 ;
}